import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import 'index.dart';
import '../../../common/routers/names.dart';
import 'widgets/index.dart';

class SettingsPage extends GetView<SettingsController> {
  const SettingsPage({super.key});

  // 主视图
  Widget _buildView() {
    return ListView(
      padding: const EdgeInsets.symmetric(vertical: 8),
      children: [
        // 帮助与支持卡片
        _buildSectionCard(
          title: '帮助与支持',
          icon: Icons.help_outline,
          children: [
            _buildSettingItem(
              icon: Icons.menu_book_outlined,
              iconColor: const Color(0xFFD84545),
              title: "用户手册",
              hasArrow: true,
              onTap: () => Get.toNamed(
                RouteNames.myPrivacyPolicy,
                arguments: {'type': 'userManual'}
              ),
            ),
            _buildDivider(),
            _buildSettingItem(
              icon: Icons.privacy_tip_outlined,
              iconColor: const Color(0xFF9C27B0),
              title: "隐私政策",
              hasArrow: true,
              onTap: () => Get.toNamed(
                RouteNames.myPrivacyPolicy,
                arguments: {'type': 'privacyPolicy'}
              ),
            ),
            _buildDivider(),
            _buildSettingItem(
              icon: Icons.report_problem_outlined,
              iconColor: const Color(0xFFFF5722),
              title: "举报信息",
              hasArrow: true,
              onTap: () => Get.to(() => const ReportPage()),
            ),
          ],
        ),
      ],
    );
  }

  /// 构建区域卡片
  Widget _buildSectionCard({
    required String title,
    required IconData icon,
    required List<Widget> children,
  }) {
    return Container(
      margin: const EdgeInsets.fromLTRB(10, 6, 10, 6),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.04),
            blurRadius: 6,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 区域标题
          Padding(
            padding: const EdgeInsets.all(12),
            child: Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(6),
                  decoration: BoxDecoration(
                    gradient: const LinearGradient(
                      colors: [Color(0xFFFF6B6B), Color(0xFFD84545)],
                    ),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Icon(
                    icon,
                    color: Colors.white,
                    size: 16,
                  ),
                ),
                const SizedBox(width: 8),
                Text(
                  title,
                  style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF333333),
                  ),
                ),
              ],
            ),
          ),
          
          // 设置项列表
          ...children,
        ],
      ),
    );
  }

  /// 构建分隔线
  Widget _buildDivider() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12),
      child: Divider(
        height: 1,
        color: Colors.grey[200],
      ),
    );
  }

  /// 构建设置项
  Widget _buildSettingItem({
    required IconData icon,
    required Color iconColor,
    required String title,
    Widget? trailing,
    bool hasArrow = false,
    VoidCallback? onTap,
  }) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(12),
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 14),
          child: Row(
            children: [
              // 图标
              Container(
                width: 32,
                height: 32,
                decoration: BoxDecoration(
                  color: iconColor.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Icon(
                  icon,
                  color: iconColor,
                  size: 18,
                ),
              ),
              const SizedBox(width: 12),
              
              // 标题
              Expanded(
                child: Text(
                  title,
                  style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.w500,
                    color: Color(0xFF333333),
                  ),
                ),
              ),
              
              // 尾部内容
              if (trailing != null) ...[
                trailing,
                const SizedBox(width: 8),
              ],
              
              // 箭头
              if (hasArrow)
                Icon(
                  Icons.chevron_right,
                  size: 20,
                  color: Colors.grey[400],
                ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SettingsController>(
      init: SettingsController(),
      id: "settings",
      builder: (_) {
        return Scaffold(
          backgroundColor: const Color(0xFFF8F9FA),
          appBar: AppBar(
            leading: IconButton(
              onPressed: () => Get.back(),
              icon: const Icon(Icons.arrow_back_ios_new_outlined),
            ),
            title: const Text(
              "设置",
              style: TextStyle(
                color: Color(0xFF333333),
                fontSize: 17,
                fontWeight: FontWeight.bold,
              ),
            ),
            centerTitle: true,
            backgroundColor: Colors.white,
            elevation: 0,
            iconTheme: const IconThemeData(color: Color(0xFF333333)),
            systemOverlayStyle: const SystemUiOverlayStyle(
              statusBarColor: Colors.transparent,
              statusBarIconBrightness: Brightness.dark,
            ),
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
